#include "BasicModel.h"
#include "BaseD3D.h"
#include "GlobalUse.h"

VBasicModel::VBasicModel(void)
{
	v_buffer = NULL;
	i_buffer = NULL;
	m_bCanDraw = TRUE;

	m_v3Position = D3DXVECTOR3(0.0f,0.0f,0.0f);
}

VBasicModel::~VBasicModel(void)
{
}

void VBasicModel::Init()
{
	VBaseD3D* baseD3D = gfGetD3DBase();

	CUSTOMVERTEX vertices[] =
	{
		{ -1.0f, 1.0f, -1.0f,	D3DCOLOR_XRGB(0, 0, 255), },    // vertex 0
		{ 1.0f, 1.0f, -1.0f,	D3DCOLOR_XRGB(192, 192, 192), },     // vertex 1
		{ -1.0f, -1.0f, -1.0f,	D3DCOLOR_XRGB(192, 192, 192), },   // 2
		{ 1.0f, -1.0f, -1.0f,	D3DCOLOR_XRGB(192, 192, 192), },  // 3
		{ -1.0f, 1.0f, 1.0f,	D3DCOLOR_XRGB(192, 192, 192), },     // ...
		{ 1.0f, 1.0f, 1.0f,		D3DCOLOR_XRGB(192, 192, 192), },
		{ -1.0f, -1.0f, 1.0f,	D3DCOLOR_XRGB(192, 192, 192), },
		{ 1.0f, -1.0f, 1.0f,	D3DCOLOR_XRGB(192, 192, 192), },
	};

    // create a vertex buffer interface called v_buffer
    baseD3D->d3ddev->CreateVertexBuffer(8*sizeof(CUSTOMVERTEX),
                               0,
                               CUSTOMFVF,
                               D3DPOOL_MANAGED,
                               &v_buffer,				//Put it to V Buffer
                               NULL);
	
    VOID* pVoid;    // a void pointer
    // lock v_buffer and load the vertices into it
    v_buffer->Lock(0, 0, (void**)&pVoid, 0);
    memcpy(pVoid, vertices, sizeof(vertices));
    v_buffer->Unlock();

	
    // create the indices using an int array
    short indices[] =
    {
        0, 1, 2,    // side 1
        2, 1, 3,
        4, 0, 6,    // side 2
        6, 0, 2,
        7, 5, 6,    // side 3
        6, 5, 4,
        3, 1, 7,    // side 4
        7, 1, 5,
        4, 5, 0,    // side 5
        0, 5, 1,
        3, 7, 2,    // side 6
        2, 7, 6,
    };

    // create an index buffer interface called i_buffer
    baseD3D->d3ddev->CreateIndexBuffer(36*sizeof(short),
                              0,
                              D3DFMT_INDEX16,
                              D3DPOOL_MANAGED,
                              &i_buffer,
                              NULL);

    // lock i_buffer and load the indices into it
    i_buffer->Lock(0, 0, (void**)&pVoid, 0);
    memcpy(pVoid, indices, sizeof(indices));
    i_buffer->Unlock();
}

void VBasicModel::SetCubeSize(float fSize) 
{
	if (fSize <= 0.0f) {
		return;
	}
	m_fCubeSize = fSize;
	Update();
}

void VBasicModel::SetCubePosition(float fPosX,float fPosY, float fPosZ) 
{
	m_v3Position.x = fPosX;
	m_v3Position.y = fPosY;
	m_v3Position.z = fPosZ;
	Update();
}

void VBasicModel::Update() 
{
	m_bCanDraw = FALSE;
	
	VBaseD3D* baseD3D = gfGetD3DBase();

	CUSTOMVERTEX vertices[] =
	{
		{ -1.0f, 1.0f, -1.0f,	D3DCOLOR_XRGB(0, 0, 255), },    // vertex 0
		{ 1.0f, 1.0f, -1.0f,	D3DCOLOR_XRGB(192, 192, 192), },     // vertex 1
		{ -1.0f, -1.0f, -1.0f,	D3DCOLOR_XRGB(192, 192, 192), },   // 2
		{ 1.0f, -1.0f, -1.0f,	D3DCOLOR_XRGB(192, 192, 192), },  // 3
		{ -1.0f, 1.0f, 1.0f,	D3DCOLOR_XRGB(192, 192, 192), },     // ...
		{ 1.0f, 1.0f, 1.0f,		D3DCOLOR_XRGB(192, 192, 192), },
		{ -1.0f, -1.0f, 1.0f,	D3DCOLOR_XRGB(192, 192, 192), },
		{ 1.0f, -1.0f, 1.0f,	D3DCOLOR_XRGB(192, 192, 192), },
	};

    // create a vertex buffer interface called v_buffer
    baseD3D->d3ddev->CreateVertexBuffer(8*sizeof(CUSTOMVERTEX),
                               0,
                               CUSTOMFVF,
                               D3DPOOL_MANAGED,
                               &v_buffer,				//Put it to V Buffer
                               NULL);
	
    VOID* pVoid;    // a void pointer
    // lock v_buffer and load the vertices into it
    v_buffer->Lock(0, 0, (void**)&pVoid, 0);
    memcpy(pVoid, vertices, sizeof(vertices));
    v_buffer->Unlock();

	m_bCanDraw = TRUE;
}

void VBasicModel::Draw()
{
	if (m_bCanDraw) {
		VBaseD3D* baseD3D = gfGetD3DBase();

		// select the vertex and index buffers to use
		baseD3D->d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX));
		baseD3D->d3ddev->SetIndices(i_buffer);

		// draw the cube
		baseD3D->d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12);
	}
}

void VBasicModel::Release()
{
    v_buffer->Release();
    i_buffer->Release();
}